package com.helwen.tools.zhijiang;

import org.apache.poi.xwpf.usermodel.*;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


/**
 * 比较两个word,
 *
 * @author Christmas_G
 *
 */
public class WordTableDiff {
    @Test
    public void readWordDiff() throws Exception {
        Map<String,String> map1=getMap("E:\\123\\全民健康\\检查报告\\个人基本信息.docx");
        Map<String,String> map2=getMap("E:\\123\\全民健康\\区域平台\\个人健康档案基本健康状况.docx");

        Iterator<Map.Entry<String,String>> entries = map1.entrySet().iterator();

        StringBuffer ceateSql=new StringBuffer("create or replace view phd_hypertension_medication as");
        while (entries.hasNext()) {
            Map.Entry<String,String> entry = entries.next();
            //System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            String key=entry.getKey();
            int flag=0;
            Iterator<Map.Entry<String,String>> entries2 = map2.entrySet().iterator();
            while (entries2.hasNext()){
                Map.Entry<String,String> obj = entries2.next();
                String name=obj.getKey();
                if(key.equals(name)){
                    flag=1;
                    entries2.remove();
                    break;
                }
            }
            if(flag==0){
                System.out.println("此字段被后面的人删除了字段名："+key);
            }
        }

        if(!map2.isEmpty()){
            for (String key : map2.keySet()) {
                System.out.println("Key = " + key);
            }
        }
    }

    private Map<String,String> getMap(String filePath) throws Exception{
        XWPFDocument document = new XWPFDocument(new FileInputStream(filePath));
        Map<String,String> map=new HashMap<>();
        try {
            // 获取word中的所有段落与表格
            List<IBodyElement> elements = document.getBodyElements();
            String protocol="";
            String[] arr={"tablename","table_name","tablename"};
            int i=0;
            for (IBodyElement element : elements) {
                // 段落
                if (element instanceof XWPFParagraph) {
                    protocol= getTableCellText((XWPFParagraph) element);
                }
                // 表格
                else if (element instanceof XWPFTable) {
                    Map<String,String> data=getTabelMapText((XWPFTable) element,arr[i]);
                    if(!data.isEmpty()){
                        map=data;
                        i++;
                    }

                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            document.close();
        }
        return map;
    }


    private String getTableCellText(XWPFParagraph paragraph) {
        // 获取段落中所有内容
        List<XWPFRun> runs = paragraph.getRuns();
        if (runs.size() == 0) {
            System.out.println("按了回车（新段落）");
            return "";
        }
        StringBuffer runText = new StringBuffer();
        for (XWPFRun run : runs) {
            runText.append(run.text());
        }
        //System.out.println(runText);

        return runText.toString();
    }

    /**
     * 获取表格内容
     *
     * @param table
     */
    private Map<String,String> getTabelMapText(XWPFTable table,String protocol) throws Exception{
        List<XWPFTableRow> rows = table.getRows();
        int x=0;
        StringBuffer ceateSql=new StringBuffer("create table HIIP.tablename(");
        StringBuffer commonSql=new StringBuffer("");
        StringBuffer xmlStr=new StringBuffer("");
        StringBuffer jsonStr=new StringBuffer("");
        Map<String,String> checkMp=new HashMap<>();
        for (XWPFTableRow row : rows) {
            if(x==0){
                x++;
                continue;
            }
            List<XWPFTableCell> cells = row.getTableCells();
            if(cells.size()<6){
                break;
            }
            List<XWPFParagraph> ycpar = cells.get(3).getParagraphs();//取第4个单元格中的值
            String ycfield=getTableCellText(ycpar.get(0));//
            //获取字段
            List<XWPFParagraph> paragraphs = cells.get(0).getParagraphs();//取第一个单元格中的值
            String path=getTableCellText(paragraphs.get(0));//
            checkMp.put(ycfield,path);

        }

        return checkMp;
    }

    private String getSpecificationWord(String word){
        if(word.length()>1){
            return word.substring(0, 1).toUpperCase()+word.substring(1);
        }else {
            return word;
        }
    }
}
